package io.renren.modules.lanling.service.impl;

import cn.hutool.core.lang.Validator;
import io.renren.common.utils.HanyuPinyinHelper;
import io.renren.modules.lanling.entity.*;
import io.renren.modules.lanling.service.AllSubjectService;
import io.renren.modules.lanling.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.lanling.dao.CertificateDao;
import io.renren.modules.lanling.service.CertificateService;


@Service("certificateService")
public class CertificateServiceImpl extends ServiceImpl<CertificateDao, CertificateEntity> implements CertificateService {

    @Autowired
    private AllSubjectService allSubjectService;
    @Autowired
    private CertificateService certificateService;

    @Override
    public List<CertificateEntity> queryList(Query query) {
        return this.baseMapper.queryList(query);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //自定义分页列表
        Query query = new Query(params);
        List<CertificateEntity> list = this.baseMapper.queryList(query);
        Integer total = this.baseMapper.queryTotal(query);

        return new PageUtils(list, total, query.getLimit(), query.getCurrPage());
    }

    @Override
    public void deleteByUserId(long studentId) {
        this.baseMapper.deleteByUserId(studentId);
    }

    @Override
    public void updateCertificate(Map<String, Object> params) {
        this.baseMapper.updateCertificate(params);
    }

    @Override
    public void deleteByAllSubjectId(List<Integer> idList) {
        this.baseMapper.deleteByAllSubjectId(idList);
    }

    @Override
    public void deleteByUserAndCert(Map<String, Object> params) {
        this.baseMapper.deleteByUserAndCert(params);
    }

    /**
     * @param newList       新的证书列表
     * @param studentEntity 学员
     */
    @Override
    public void dealWithCert(List<Integer> newList, StudentEntity studentEntity) {
        //学员信息
        HanyuPinyinHelper hanyuPinyinHelper = new HanyuPinyinHelper();
        String pName = hanyuPinyinHelper.toHanyuPinyin(studentEntity.getRealname());


        //todo 查询旧的证书列表，返回的是List<CertificateEntity>

        EntityWrapper<CertificateEntity> entityWrapper = new EntityWrapper<>();

        List<CertificateEntity> certificateEntityList = this.baseMapper.selectList(entityWrapper);

        List<Integer> certList = new ArrayList<>();
        if (certificateEntityList.size() > 0) {
            for (CertificateEntity certificateEntity : certificateEntityList) {
                Integer allSubjectId = certificateEntity.getAllSubjectId();
                certList.add(allSubjectId);
            }
        }

        //todo 旧的遍历新的，代表的是删减
        if (certList.size() > 0) {
            for (Integer allSubjectId : certList) {
                if (!newList.contains(allSubjectId)) {
                    entityWrapper = new EntityWrapper<>();
                    entityWrapper.eq("student_id", studentEntity.getUserId());
                    entityWrapper.eq("all_subject_id", allSubjectId);
                    Integer integer = this.baseMapper.selectCount(entityWrapper);
                    if (integer > 0) {
                        this.baseMapper.delete(entityWrapper);
                    }
                }
            }
        }

        //todo 新的遍历旧的，代表的是新增
        if (newList.size() > 0) {
            for (Integer allSubjectId : newList) {
                entityWrapper = new EntityWrapper<>();
                entityWrapper.eq("student_id", studentEntity.getUserId());
                entityWrapper.eq("all_subject_id", allSubjectId);
                Integer integer = this.baseMapper.selectCount(entityWrapper);
                if (integer == 0) {
                    AllSubjectEntity allSubjectEntity = allSubjectService.selectById(allSubjectId);
                    String certificateName = allSubjectEntity.getCertificateName();
                    CertificateEntity certificateEntity = new CertificateEntity();
                    certificateEntity.setUserName(studentEntity.getRealname());
                    certificateEntity.setStudentId(studentEntity.getUserId());
                    certificateEntity.setStudentPname(pName);
                    certificateEntity.setCertificateName(certificateName);
                    certificateEntity.setSchoolId(studentEntity.getSchoolId());
                    certificateEntity.setCreateDate(new Date());
                    certificateEntity.setSex(studentEntity.getSex());
                    certificateEntity.setIdCard(studentEntity.getIdCard());
                    certificateEntity.setAllSubjectId(allSubjectId);
                    certificateEntity.setExamState(3);
                    certificateService.insert(certificateEntity);
                }
            }
        }

    }

}
